<?php
/*
  Plugin Name: Watchlist
  Plugin URI: http://www.osclass.org/
  Description: This plugin add possibility for user to watch items.
  Version: 1.0.3
  Author: Richard Martin (keny)
  Author URI: http://www.proodi.com
  Author Email: keny10@gmail.com
  Short Name: WatchList
  Plugin update URI: http://www.osclass.org/
 */

    define('WATCHLIST_VERSION', '1.0.3') ;

    /*function watchlist() {
        echo '<a href="javascript://" class="watchlist" id="' . osc_item_id() . '">' ;
        echo '<span>' . __('Add to watchlist', 'watchlist') . '</span>' ;
        echo '</a>' ;
    }*/
	
	function watchlist() {
        $id    = Params::getParam('id');
	    if ( osc_is_web_user_logged_in() ) {
            //check if the item is not already in the watchlist
            $conn   = getConnection();
            $detail = $conn->osc_dbFetchResult("SELECT * FROM %st_item_watchlist WHERE fk_i_item_id = %d and fk_i_user_id = %d", DB_TABLE_PREFIX, $id, osc_logged_user_id());

            //If nothing returned then we can process
            if (!isset($detail['fk_i_item_id'])) {
                           
                echo '<a href="javascript://" class="watchlist" id="' . osc_item_id() . '">' ;
       			echo '<img src="'. osc_current_web_theme_url('images/star.png') .'" /><span style="margin-left:5px;color:#333333;font-weight:bold;">' . __('Favorite', 'watchlist') . '</span>' ;
       			echo '</a>' ;
					
               
            } 
			else 
				{
                //Already in watchlist !
                echo '<span align="left">				
				<img src="'. osc_current_web_theme_url('images/star_activated.png') .'" />
				<span style="margin-left:4px;color:#AA7700;font-weight:bold;">
				' . __('Favorite','watchlist') . ' </span>
				<a href="' . osc_base_url(true) . '?page=custom&file=watchlist/watchlist.php"><span style="color:#6A6A6A;">[' . __('view my favorites','watchlist') . ']</span></a>
				</span>';
            	}
        } 
		else {
            //error user is not login in
            //echo '<a href="' . osc_user_login_url() . '">' . __('Please login', 'watchlist') . '</a>';
			 echo '<a href="javascript://" class="watchlist" id="' . osc_item_id() . '">' ;
       		echo '<img src="'. osc_current_web_theme_url('images/star.png') .'" /><span style="margin-left:5px;color:#333333;font-weight:bold;">' . __('Favorite', 'watchlist') . '</span>' ;
       		echo '</a>' ;
        }
		
	    		 
    }

    function watchlist_user_menu() {	
		if (stripos($_SERVER['REQUEST_URI'],'watchlist') !== false) echo '<li class="active opt_favorites">';
		else echo '<li class="opt_favorites">';
		echo '<a href="' . osc_render_file_url(osc_plugin_folder(__FILE__) . 'watchlist.php') . '" >' . __('Manage your favorites', 'watchlist') . '</a></li>' ;
    }
	
	function osc_is_user_favorites() {
            $location = Rewrite::newInstance()->get_location();
            $section  = Rewrite::newInstance()->get_section();
            if(strpos($location, "watchlist")){
                return true;
            }
            return false;
        }

    function watchlist_call_after_install() {
        $conn = getConnection() ;
        $path = osc_plugin_resource('watchlist/struct.sql') ;
        $sql  = file_get_contents($path) ;
        $conn->osc_dbImportSQL($sql) ;
    }

    function watchlist_call_after_uninstall() {
        $conn = getConnection() ;
        $conn->osc_dbExec('DROP TABLE %st_item_watchlist', DB_TABLE_PREFIX) ;
    }

    function watchlist_header() {
        echo '<!-- Watchlist js -->' ;
        echo '<script type="text/javascript">' ;
        echo 'var watchlist_url = "' . osc_ajax_plugin_url('watchlist/ajax_watchlist.php') . '" ;' ;
        echo '</script>' ;
        echo '<script type="text/javascript" src="' . osc_plugin_url('watchlist/js/watchlist.js') . 'watchlist.js"></script>' ;
        echo '<!-- Watchlist js end -->' ;
    }

    function watchlist_delete_item($item) {
        $conn = getConnection() ;
        $conn->osc_dbExec("DELETE FROM %st_item_watchlist WHERE fk_i_item_id = '$item'", DB_TABLE_PREFIX) ;
    }

    function watchlist_help() {
        osc_admin_render_plugin(osc_plugin_path(dirname(__FILE__)) . '/help.php') ;
    }

    // This is needed in order to be able to activate the plugin
    osc_register_plugin(osc_plugin_path(__FILE__), 'watchlist_call_after_install') ;

    // This is a hack to show a Uninstall link at plugins table (you could also use some other hook to show a custom option panel)
    osc_add_hook(osc_plugin_path(__FILE__) . '_uninstall', 'watchlist_call_after_uninstall') ;

    // This is a hack to show a Configure link at plugins table (you could also use some other hook to show a custom option panel)
    osc_add_hook(osc_plugin_path(__FILE__) . '_configure', 'watchlist_help') ;

    // Add link in user menu page
    osc_add_hook('user_menu', 'watchlist_user_menu') ;

    // add javascript
    osc_add_hook('header', 'watchlist_header') ;

    //Delete item
    osc_add_hook('delete_item', 'watchlist_delete_item') ;

?>